package com.evil.application.pojo.dto.form.control;

import com.evil.application.enums.form.control.ControlCodeEnum;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 表单控件条件 返回实体
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@Data
public class ControlConditionRespDTO {

    /**
     * 控件id(Long)/判断符id(Integer)
     */
    private Object id;

    /**
     * 控件code
     */
    private String controlCode;

    /**
     * 控件名称/判断符名称
     */
    private String name;

    /**
     * 是否属于表格子控件
     */
    private Boolean beingTableChild;

    /**
     * 上级id
     */
    private Long parentId;

    /**
     * 子级
     */
    private List<ControlConditionRespDTO> children;

    public ControlConditionRespDTO() {
        this.beingTableChild = false;
        this.children = new ArrayList<>();
    }

    public ControlConditionRespDTO(ControlBaseInfoDTO controlBase) {
        this.id = controlBase.getControlId();
        this.controlCode = controlBase.getControlCode();
        this.name = controlBase.getControlTitle();
        this.beingTableChild = false;

        ControlCodeEnum controlCodeEnum = ControlCodeEnum.findByCode(this.controlCode);
        this.children = controlCodeEnum.getJudgeSigns().getAll().stream().map(ControlConditionRespDTO::new).collect(Collectors.toList());
    }

    public ControlConditionRespDTO(ControlBaseInfoDTO controlBase, ControlBaseInfoDTO childControlBase) {
        this.id = childControlBase.getControlId();
        this.controlCode = childControlBase.getControlCode();
        this.name = String.format("%s · %s", controlBase.getControlTitle(), childControlBase.getControlTitle());
        this.beingTableChild = true;
        this.parentId = controlBase.getControlId();

        ControlCodeEnum controlCodeEnum = ControlCodeEnum.findByCode(this.controlCode);
        this.children = controlCodeEnum.getJudgeSigns().getAll().stream().map(ControlConditionRespDTO::new).collect(Collectors.toList());
    }

    public ControlConditionRespDTO(ControlJudgeSignRespDTO controlJudgeSignRespDTO) {
        this.id = controlJudgeSignRespDTO.getId();
        this.name = controlJudgeSignRespDTO.getName();
        this.beingTableChild = false;
        this.children = new ArrayList<>();
    }
}
